const cheerio = require('cheerio')
const fs = require('fs')

const {Jcrapy, Request, Item, Pipeline} = require('../jcrapy/jcrapy')

class DouBanBook extends Jcrapy {
  constructor() {
    super()
    this.pipeline = new DouBanBookPipeline()
  }

  * startRequests(self) {
    let page = 0
    for (let i = 0; i < 10; i++) {
      let url = `https://book.douban.com/top250?start=${page * 25}`
      yield new Request({url})
      page++
    }
  }

  * parse(response) {
    let html = response.text
    let $ = cheerio.load(html)
    let $books = $('div.article tr.item > td:nth-child(2)')
    for (let i = 0; i < $books.length; i++) {
      let $book = cheerio.load($books[i])
      let title = $book('div.pl2 > a').attr('title').trim()
      let extra = $book('p.pl').text().trim()
      let brief = $book('span.inq').text().trim()
      let item = new Item()
      item.title = title
      item.extra = extra
      item.brief = brief
      console.log(item)
      yield item
    }
  }
}

/**
 * pipeline
 */
class DouBanBookPipeline extends Pipeline {
  constructor() {
    super()
    this.writeStream = fs.createWriteStream('dou_gan_book.csv')
  }

  processItem(item, spider) {
    let line = `${item.title},${item.extra},${item.brief}\n`
    this.writeStream.write(line)
    return item
  }
}

doubanbook = new DouBanBook()
doubanbook.run()
